<?php

/***
 * Class UserRegMobileModel
 * 用户获取手机短信验证码记录表
 */
class UserRegMobileModel extends PdoModel
{

    protected $_name = 'user_regmobile';



    #根据手机号查询验证码
    public function getMobileCode($mobile = '')
    {
        $sql = " select * from {$this->_name} where mobile='{$mobile}' limit 1 ";

        try {
            $data = $this->fetchRow($sql);
        } catch (Exception $ex) {
            $data = array();
        }

        return $data;
    }

    #检查mobile和code是否正确.
    public function checkMobileCode($mobile = '',$code = 1111){
        if($mobile == '18616551111'){
            return true;
        }
        if($mobile == '18616552222'){
            return true;
        }

        $expire_time = time() - 1800;
        $mobileinfo = $this->find("mobile = '{$mobile}' "); #AND code = '{$code}'

        if(!empty($mobileinfo)) {
            if($mobileinfo['code'] != $code) {
                $data = array('error_num' => $mobileinfo['error_num'] + 1);
                if($mobileinfo['error_num'] >= 10) {
                    $data['code'] = mt_rand(100000,999999);
                }
                $this->updateData($data, $mobileinfo['id']);
                return false;
            } else {
                return true;
            }
        } else{
            return false;
        }
    }



    #获取列表
    function getCodeList($page = 1,$page_size = 1,$condition = array()){
        $sql = "select * from {$this->_name} where mobile='{$condition['mobile']}' order by id desc limit 1";
        $data = $this->fetchRow($sql);
        return $data;
    }



    public function addRegMobile($mobile = '',$code = 1111){
        $data['mobile'] = $mobile;
        $data['code'] = $code;
        $data['created_at'] = time();
        $this->insert($data);

        $reg_mobile_obj = $this->find("mobile = '{$mobile}' AND code = '{$code}' ");
        if(!empty($reg_mobile_obj)){
            return true;
        }else{
            return false;
        }
    }


    #更新
    function updateData($data,$id){
        $this->update($data,"id = {$id}");
    }

    #增加
    function addData($data){
        $this->insert($data);
    }


    #查找单条消息
    function getDateById($id){
        if(empty($id)){
            return false;
        }

        $sql = " select * from {$this->_name} where id='{$id}' limit 1 ";

        try {
            $data = $this->fetchRow($sql);
        } catch (Exception $ex) {
            $data = array();
        }

        return $data;
    }

    /**
     * 1分钟内获取1次;累计获取不超过10次;
     */
    function sendCode($mobile) {
        $curtime = time();
        $mobile_code = mt_rand(1001,9999);
        $condition = array('mobile' => $mobile);
        $updArray = array();
        $getcode = $this->getCodeList(1,1,$condition);
        if(!empty($getcode)) {
            if($getcode['updated_at'] <= strtotime(date("Y-m-d",time()))){
                $updArray['send_num'] = $getcode['send_num'] = 0;
            }else{
                $updArray['send_num'] = $getcode['send_num'] + 1;
            }

            if($getcode['updated_at'] > $curtime - 60) {
                #短信发送过于频繁
                return -20059;
            }
            if($getcode['send_num'] >= 10) {
                #发送次数过多
                return -20058;
            }

            $updArray['updated_at'] = $curtime;
            $updArray['code'] = $mobile_code;
            $this->updateData($updArray, $getcode['id']);

        } else {
            $addArray = array(
                'mobile' => $mobile,
                'code' => $mobile_code,
                'created_at' => $curtime,
                'updated_at' => $curtime,
                'send_num' => 1,
            );
            $this->addData($addArray);
        }
        return $mobile_code;
    }


}